package malus.shaofeng.com.beiwomusic;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import malus.shaofeng.com.beiwomusic.utils.PlayTimeUtil;
import malus.shaofeng.com.beiwomusic.utils.Player;

public class MusicServer extends Service implements MediaPlayer.OnBufferingUpdateListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {

    public MediaPlayer mediaPlayer;
 //   private OnProgressListener onProgressListener;
    private Timer timer=new Timer();
    private int currentPosition;
    private int duration;

    private Intent intent;

    private String url_origin="";
    private boolean playing;

    @Override
    public IBinder onBind(Intent intent) {
      //  return null;
        return new MyController();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        intent=new Intent("malus.shaofeng.com.beiwomusic.MUSIC_RECEIVE");
        mediaPlayer=new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);

        timer.schedule(timerTask,0,1000);
    }

        TimerTask timerTask=new TimerTask() {
        @Override
        public void run() {
            if (mediaPlayer==null){
                return;
            }else
                handler.sendEmptyMessage(0);
//            if (mediaPlayer.isPlaying()){
//                handler.sendEmptyMessage(0);
//            }
        }
    };

    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            currentPosition = mediaPlayer.getCurrentPosition();
            duration = mediaPlayer.getDuration();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    intent.putExtra("serviceCurrent", currentPosition);
                    intent.putExtra("serviceDuration",duration);
                    playing = mediaPlayer.isPlaying();
                    intent.putExtra("isPlaying", playing);
                    intent.putExtra("url",url_origin);
                    //设置发送广播的类型，可以随便写一个

                    sendBroadcast(intent);
                }
            }).start();

//            if (onProgressListener!=null){
//                onProgressListener.onProgress(currentPosition,duration);
//            }

        }
    };


    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {

//        String path = intent.getStringExtra("path");
//        mediaPlayer.reset();
//        try {
//            mediaPlayer.setDataSource(path);
//            mediaPlayer.prepare();
//            mediaPlayer.start();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
    //    seekBar.setSecondaryProgress(percent);
  //      int currentProgress = seekBar.getMax()
  //              * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
    }



    @Override
    public void onCompletion(MediaPlayer mp) {

        intent.putExtra("serviceCurrent", 0);
        intent.putExtra("serviceDuration",duration);
        playing = mediaPlayer.isPlaying();
        intent.putExtra("isPlaying", playing);
        intent.putExtra("url",url_origin);
        sendBroadcast(intent);
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    public class MyController extends Binder{


        public void seekto(int progress){
            mediaPlayer.seekTo(progress);
        }

        public boolean isMediaNull(){
            return  mediaPlayer==null;
        }

        public boolean isPlayed(){
            if (mediaPlayer.isPlaying()){
                return true;
            }
            else {
                return false;
            }
        }


        public void playUrl(String url){

            if (mediaPlayer==null)
                return;

            if(url_origin.equals(url)){
                return;
            }
            url_origin=url;
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(url_origin);
                mediaPlayer.prepareAsync();
     //           mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void play(){
            mediaPlayer.start();
        }

        public void pause(){
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
        }

        public void stop(){
            if (mediaPlayer!=null){
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer=null;
            }
        }

 //       public MusicServer getService(){
  //          return MusicServer.this;
  //      }

    }

//    public interface OnProgressListener {
//        void onProgress(int current,int duration);
//    }

//    public void setOnProgressListener(OnProgressListener onProgressListener) {
//        this.onProgressListener = onProgressListener;
//    }



}


